/*
 *  描述：在特定区域内显示事先定好的颜色
    知识点：
        1. shader的基本结构
        2. 基本流程 VS和FS
        3. uniform/varying
        4. params 中的 type类型
    拓展：门向外，心跳动

*/
const renderEngine = cc.renderer.renderEngine;
const renderer = renderEngine.renderer;

var shader = {
    name: "01_Eraser",

    params: [
        {name: 'clearColor', type: renderer.PARAM_COLOR4},
    ],

    defines: [],

    start(sprite, material) {
        var clearColor = {r: 1, g: 0, b: 1, a: 0.5};
        material.setParamValue("clearColor", clearColor)
    },

    update(sprite, material) {
        
    },

    vert: `uniform mat4 viewProj;
        attribute vec3 a_position;
        attribute vec2 a_uv0;
        varying vec2 uv0;
        void main () {
            vec4 pos = viewProj * vec4(a_position, 1);
            gl_Position = pos;
            uv0 = a_uv0;
        }
        `,

    frag: `uniform sampler2D texture;
        uniform vec4 color;
        uniform vec4 clearColor;
        varying vec2 uv0;

        void main()
        {
            vec4 c = color * texture2D(texture,uv0);
            float length = 0.1;
            if(uv0.x < (0.5 + length) && uv0.x > (0.5 - length) &&
                uv0.y < (0.5 + length) && uv0.y > (0.5 - length)){
                gl_FragColor = clearColor;
            }else{
                gl_FragColor = c;
            };
        }`,
}

let CustomMaterial = require('CustomMaterial');
CustomMaterial.addShader(shader);